<template>
  <div class="login">
    <div class="top">
      <p class="toptitle">应急指挥信息平台</p>
    </div>
    <div class="content">
      <div class="contentbg">
        <div class="numberbg"></div>
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
          <h3 class="title">登录系统</h3>
          <p class="loginname">账号登录</p>
          <el-form-item prop="username" style="margin-top: 30px;">
            <p style="color: #fff;margin: 0;">
              <img src="../../public/img/luser.png" style="margin-right: 10px;" />
              账号
            </p>
            <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <p style="color: #fff;margin: 0;">
              <img src="../../public/img/lpass.png" style="margin-right: 10px;" />
              密码
            </p>
            <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码" @keyup.enter.native="handleLogin"></el-input>
          </el-form-item>
          <!--<el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>-->
          <el-form-item style="width:100%;">
            <el-button :loading="loading" size="medium" type="primary" style="width:100%;color: #fff;" @click.native.prevent="handleLogin">
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <!--  底部  -->
    <!--<div class="el-login-footer">
      <span>Copyright © 2018-2019 ruoyi.vip All Rights Reserved.</span>
    </div>-->
  </div>
</template>

<script>
import { getCodeImg } from '@/api/login';
import Cookies from 'js-cookie';
import { encrypt, decrypt } from '@/utils/jsencrypt';

export default {
  name: 'Login',
  data() {
    return {
      codeUrl: '',
      cookiePassword: '',
      loginForm: {
        username: '',
        password: '',
        rememberMe: false,
        code: '',
        uuid: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', message: '用户名不能为空' }],
        password: [{ required: true, trigger: 'blur', message: '密码不能为空' }],
        code: [{ required: true, trigger: 'change', message: '验证码不能为空' }]
      },
      loading: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.codeUrl = 'data:image/gif;base64,' + res.img;
        this.loginForm.uuid = res.uuid;
      });
    },
    getCookie() {
      const username = Cookies.get('username');
      const password = Cookies.get('password');
      const rememberMe = Cookies.get('rememberMe');
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set('username', this.loginForm.username, { expires: 30 });
            Cookies.set('password', encrypt(this.loginForm.password), {
              expires: 30
            });
            Cookies.set('rememberMe', this.loginForm.rememberMe, {
              expires: 30
            });
          } else {
            Cookies.remove('username');
            Cookies.remove('password');
            Cookies.remove('rememberMe');
          }
          this.$store
            .dispatch('Login', this.loginForm)
            .then(() => {             
              this.$router.push({ path: this.redirect || '/' });
            })
            .catch(() => {
              this.loading = false;
              this.getCode();
            });
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.login {
  /*display: flex;
		justify-content: center;
		align-items: center;*/
  height: 100%;
  background: #030303;
  background: url('../assets/image/bgmap.png');
  background-size: cover;
}

#container {
  position: absolute;
  top: 107px;
  bottom: 0;
  width: 100%;
}

.top {
  height: 107px;
  background-image: url('../assets/image/ltop.png');
  background-size: cover;
}
.toptitle {
  height: 107px;
  line-height: 80px;
  text-align: center;
  color: #fff;
  font-size: 30px;
  margin: 0;
}
.title {
  margin: 22px auto 30px auto;
  text-align: center;
  color: #00a8ff;
  font-size: 30px;
}
.content {
  height: calc(100% - 107px);
  padding: 0 60px 40px;
  .contentbg {
    height: 100%;
    background: url('../assets/image/lbgframe.png') no-repeat;
    background-size: 100% 100%;
    position: relative;
  }
}
.numberbg {
  position: absolute;
  top: 31%;
  right: 20%;
  width: 542px;
  height: 297px;
  // background-image: url('../assets/image/lnumber.png');
  // background-size: cover;
}
.login-form {
  position: absolute;
  top: 20%;
  right: 5%;
  border-radius: 6px;
  width: 470px;
  height: 470px;
  background-image: url('../assets/image/bg.png');
  background-size: cover;
  padding: 25px 60px 5px 60px;
  .loginname {
    color: #fff;
    border-left: 4px solid #00a8ff;
    padding-left: 20px;
  }
  input {
    background: transparent;
    border: 1px solid #9ce6ff;
    color: #fff;
  }
  .el-button--primary {
    color: #000;
    background-color: #00a8ff;
    border-color: #00a8ff;
    height: 50px;
    font-size: 16px;
  }
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 38px;
}
</style>
